package ah.hsqldb;

import java.io.File;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TestHibernate {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Configuration cfg = null;
		SessionFactory sf = null;
		Session s = null;
		Transaction tran = null;
		try {
			
			cfg = new Configuration().configure(new File("src/hsqldb/hibernate.cfg.xml"));
			sf = cfg.buildSessionFactory();
			s = sf.openSession();
			tran = s.beginTransaction();
			
			Query q = s.createSQLQuery("create table ccc(id integer,name varchar)");
			q = s.createSQLQuery("insert into ccc(id,name) values(1,'ddh')");
			q = s.createSQLQuery("select * from ccc");
			
			//q.executeUpdate();
			
			List list = q.list();
			for (Object obj : list) {
				Object[] o = (Object[])obj;
				System.out.println(o[0] + ", " + o[1]);
			}
			
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tran != null)
				tran.rollback();
		} finally {
            if(s!=null && s.isOpen()) s.close();
            if(sf!=null && !sf.isClosed()) sf.close();
		}
		

	}

}
